$(function() {
	admin.role.init();
});
Namespace('admin.role',{
	init:function() {
		easyui.util.initDatagrid('#datagrid',{
			onDblClickRow:function() {
				admin.role.update();
			}
		});
	},
	query:function() {
		if(!$('#query-form').form('validate')) return;
		var data=$('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load',data);
	},
	update:function() {
		
		var row = $('#datagrid').datagrid('getSelected');
		if(row==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		getDialog('data-dialog').dialog({
			href:contextPath+'/sys/role/update/'+row.id,
			title: '修改角色',
		    width: 500,
		    height: 450,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.role.doUpdate();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#data-dialog').dialog('close');
				}
			}]
		});
	},
	doUpdate:function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		$('#data-dialog').dialog('close');
		
		$('#datagrid').datagrid('loading');
		var row = $('#datagrid').datagrid('getSelected');
		admin.role.setCheckedFunction(data,'#function-tree');
		admin.role.setUncheckdFunction(data,'#function-tree');
		$.post(contextPath+'/sys/role/update/'+row.id,data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==1) {
				top.showInfo('修改成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('修改失败!');
			}
		});
	},
	add:function() {
		getDialog('data-dialog').dialog({
			href:contextPath+'/sys/role/add',
			title: '添加角色',
		    width: 500,
		    height: 450,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.role.doAdd();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#data-dialog').dialog('close');
				}
			}]
		});
	},
	doAdd:function() {
		if(!$('#data-form').form('validate')) return;
		var data = $('#data-form').form('jsonObject');
		$('#data-dialog').dialog('close');
		$('#datagrid').datagrid('loading');
		admin.role.setCheckedFunction(data,'#function-tree');
		$.post(contextPath+'/sys/role/add',data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==1) {
				top.showInfo('添加成功!');
				$('#datagrid').datagrid('reload');
			} else {
				top.showInfo('添加失败!');
			}
		});
	},
	selectTab:function(title,functree) {
		if(title!='分配功能') return;
		if($(functree).data('loaded')) return;	//如果已经加载了角色树,那么无需再加载
	
		$(functree).tree({
			checkbox: true,
			animate:true,
			onlyLeafCheck:true,
			onClick:function(node){
				if(node.attributes.type==1) {
					$(this).tree('toggle', node.target);
				} else {
					if($(node.target).find('.tree-checkbox0').size()>0) {
						$(this).tree('check', node.target);
					} else {
						$(this).tree('uncheck', node.target);
					}
				}
			},
			onLoadSuccess:function() {
				$(functree).data('loaded',true);
			}
		});
	},
	/**
	 * 设置已选择的功能节点
	 * @param role
	 * @param jqtree
	 */
	setCheckedFunction:function(role,jqtree) {
		if ($(jqtree).data('loaded')) {
			var checkedNodes=$(jqtree).tree('getChecked');
			var funcId=$.map(checkedNodes,function(node) {
				return node.id;
			});
			console.info('+++++++++++++++++++++++++:'+funcId);
			role.functionIds = funcId.join(',');
		}
	},
	/**
	 * 设置未选择的功能节点
	 * @param role
	 * @param jqtree
	 */
	setUncheckdFunction:function(role,jqtree) {
		if($(jqtree).data('loaded')) {
			var uncheckedNodes=$(jqtree).tree('getChecked','unchecked');
			var funcId=$.map(uncheckedNodes,function(node) {
				return node.id;
			});
			console.info('+++++++++++++++++++++++++uncheckdFunctionId:'+funcId);
			role.uncheckdFunctionId=funcId.join(',');
		}
	},
	/**
	 * 刷新角色与菜单的关系
	 */
	refreshMenu:function() {
		$.post(contextPath+'/sys/role/refresh',{},function(result) {
			if(result.code==1) {
				top.showInfo('刷新成功!');
			} else {
				top.showInfo('添加失败!');
			}
		});
	}
});